<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $uses = array(
		'Menus',
		'Advertise',
		'Category',
		'Support',
		'Menus',
	);
	public $paginate = array(
		'limit' => 5,
		'order' => array('User.id' => 'Desc')
	);
	public $helpers = array('Js');
	/**
	 * Components
	 * @var array
	 */
	public $components = array(
		'RequestHandler',
		'Common',
		'Breadcrumb',
	);

	private $menu_multi = array();
	private $stt = -1;

	public function beforeFilter(){
		parent::beforeFilter();
		$listMenu = $this->Menus->getListMenu();
		$menu_level = array();
		foreach ($listMenu as $key => $menu){
			$menus = $menu['menus_multi'];
			$menu_level[$menus['mnu_parent_id']][$menus['id']] = $menus;
		}

		$this->_sortLevel($menu_level);
		//get all category product display box left
		$category_product = $this->Category->getListCategoryType("'products'");

		//get all banner
		$allBanner = array();
		$banners = $this->Advertise->getAllBanner();
		foreach ($banners as $banner){
			$allBanner[] = $banner['advertise'];
		}
		$allBanner = CommonComponent::classifyArray($allBanner, 'ban_type');

		//get all support online
		$support = $this->Support->getAllSupport();
		//get menu
		$menu = $this->Menus->getListMenu();
		//breadcrumb for TOP page
		$uri_base = Router::url( $this->here, true );
		BreadcrumbComponent::set('Trang chu', '/');

		//set view
		$this->set(
			array(
				'list_menu' => $this->menu_multi,
				'category_product' => $category_product,
				'all_banner' => $allBanner,
				'support_online' => $support,
				'menu_top' => $menu,
			)
		);
	}

	/**
	* Sap xep danh muc theo level
	*
	* @param array $arrayCategory
	* @param number $keystart nút cha
	* @param number $level
	*/
	private function _sortLevel($arrayCategory, $keystart = 0, $level = -1){
		//kiểm tra xem tồn tại record không
		if (array_key_exists($keystart, $arrayCategory)) {
			$level++;
			foreach ($arrayCategory[$keystart] as $key => $value){

				//gán các phần tử cho array menu sắp xếp theo đúng vị trí
				//tang so thu tu
				$this->stt++;
				$this->menu_multi[$this->stt] = $value;

				//gan level cho menu
				$this->menu_multi[$this->stt]['level'] = $level;

				//thiet lap de biet day la` 1 nut cha
				if (array_key_exists($key,$arrayCategory)){
					$this->menu_multi[$this->stt]["parent"] = 1;
				} else {
					$this->menu_multi[$this->stt]["parent"] = 0;
				}

				//de quy de lap lai, neu menu_id nam trong array cac menu cha
				$this->_sortLevel($arrayCategory, $key, $level);
			}
		}
	}


}
